<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '增加'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="24">
					<a-form-item label="流程标题：" name="flowTitle">
						<a-input v-model:value="formData.flowTitle" placeholder="请输入流程标题" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="流程类型：" name="flowType">
						<a-select v-model:value="formData.flowType" placeholder="请选择流程类型" :options="flowTypeOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="流程图标：" name="flowIcon">
						<a-input
							v-model:value="formData.flowIcon"
							style="width: calc(100% - 70px)"
							placeholder="请选择图标"
							disabled
						/>
						<a-button type="primary" @click="iconSelector.showIconModal(formData.icon)">选择</a-button>
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="流程表单：" name="formId">
						<a-select v-model:value="formData.formId" placeholder="请选择流程表单" :options="formIdOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="排序：" name="sort">
						<a-input v-model:value="formData.sort" type="Number" placeholder="请输入排序" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="描述：" name="remark">
						<a-textarea
							v-model:value="formData.remark"
							placeholder="请输入描述"
							:auto-size="{ minRows: 3, maxRows: 5 }"
							allow-clear
						/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</xn-form-container>
</template>

<script setup name="sysFlowConfigForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import sysFlowConfigApi from '@/api/flow/sysFlowConfigApi'
	import sysFromConfigApi from '@/api/flow/sysFromConfigApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const flowTypeOptions = ref([])
	const fromTypeOptions = ref([])
	const formIdOptions = ref([])
	let iconSelector = ref()

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		flowTypeOptions.value = tool.dictList('FLOW_TYPE')
		sysFromConfigApi.sysFromConfigSelectItems().then((res) => {
			formIdOptions.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		flowTitle: [required('请输入流程标题')],
		flowType: [required('请输入流程类型')],
		flowIcon: [required('请选择流程图标')],
		formId: [required('请选择流程表单')]
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.flowIcon = value
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			sysFlowConfigApi
				.sysFlowConfigSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
